<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      small
      :hide-on-single-page="true"
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :pager-count="pagerCount"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="text-align: center;"
    />
  </div>
</template>

<script>
// import { scrollTo } from '@/utils/scroll-to'

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "pagination",
  props: {
    total: {
      required: true,
      type: Number,
    },
    page: {
      type: Number,
      default: 1,
    },
    limit: {
      type: Number,
      default: 20,
    },
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 30, 50];
      },
    },
    // 移动端页码按钮的数量端默认值5
    pagerCount: {
      type: Number,
      // default: 3,
      default: document.body.clientWidth < 992 ? 5 : 7,
    },
    layout: {
      type: String,
      default: "total, sizes, prev, pager, next, jumper",
    },
    background: {
      type: Boolean,
      default: true,
    },
    autoScroll: {
      type: Boolean,
      default: true,
    },
    hidden: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {};
  },
  computed: {
    currentPage: {
      get() {
        return this.page;
      },
      set(val) {
        this.$emit("update:page", val);
      },
    },
    pageSize: {
      get() {
        return this.limit;
      },
      set(val) {
        this.$emit("update:limit", val);
      },
    },
  },
  methods: {
    handleSizeChange(val) {
      if (this.currentPage * val > this.total) {
        this.currentPage = 1;
      }
      this.$emit("pagination", { page: this.currentPage, limit: val });
      // if (this.autoScroll) {
      //   scrollTo(0, 800)
      // }
    },
    handleCurrentChange(val) {
      this.$emit("pagination", { page: val, limit: this.pageSize });
      // if (this.autoScroll) {
      //   scrollTo(0, 800)
      // }
    },
  },
};
</script>

<style scoped lang="scss">
.pagination-container {
  // background: #fff;
  // padding: 32px 16px;
  margin: 0 auto;
  margin-top: .5vh;
  ::v-deep .el-pagination.is-background .btn-next,
  ::v-deep .el-pagination.is-background .btn-prev {
    margin: 0 5px;
    border: 1px solid #05409c;
    background: #0b1f3b;
    color: #fff;
    min-width: 1.2vw;
    border-radius: 2px;
  }
  ::v-deep .el-pagination button {
    min-width: 1.2vw;
    height: 2.5vh;
    line-height: 2.5vh;
  }
  ::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active {
    border: 1px solid #05409c;
    background: #05409c;
  }
  ::v-deep .el-pagination.is-background .el-pager li {
    background-color: #0b1f3b;
    border: 1px solid #05409c;
    color: #fff;
    min-width: 1.2vw;
  }
  ::v-deep .el-pager li {
    background: #0b1f3b;
    min-width: 1.2vw;
    height: 2.5vh;
    line-height: 2.5vh;
  }
  ::v-deep .el-pager .el-icon-more {
    background: transparent !important;
    border: none !important;
  }
  ::v-deep .el-pager .more::before {
    color: #05409c;
  }
  ::v-deep .el-pagination__jump {
    margin-left: 0.6vw;
    font-weight: 400;
    color: #fff;
  }
}
.pagination-container.hidden {
  display: none;
}
</style>
